package com.newbie.iSee;

import com.facebook.android.SessionEvents;
import com.facebook.android.SessionStore;
import com.facebook.android.Facebook;
import com.facebook.android.LoginButton;
import com.facebook.android.Util;
import com.facebook.android.SessionEvents.AuthListener;
import com.facebook.android.SessionEvents.LogoutListener;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log; /*
 import android.view.View;
 import android.widget.EditText;
 import android.widget.TextView;
 */
import android.widget.TextView;
import android.widget.Toast;

public class LoginPage extends Activity {
	private Context _context;
	// private LoginButton _btLogin;
	// private EditText _etID, _etPW;

	public static final String APP_ID = "138049249584373";
    private static final String[] PERMISSIONS =
        new String[] {"friends_photos", "publish_stream", "read_stream", "offline_access", "user_photos"};
    private LoginButton mLoginButton;
    private TextView mText;
    private String _from;
    //private TextView mText;

	private Facebook mFacebook;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Log.d("++++++", "------------start login page");
		
		Bundle extras = getIntent().getExtras();
		_from = extras.getString("from");

		init();
		
		mFacebook = new Facebook(APP_ID);
		
		mText = (TextView) this.findViewById(R.id.login_title);
        //SessionStore.restore(mFacebook, this);
        SessionEvents.addAuthListener(new SampleAuthListener());
        SessionEvents.addLogoutListener(new SampleLogoutListener());
        mLoginButton = (LoginButton) this.findViewById(R.id.login_login);
        mLoginButton.setBackgroundDrawable(getResources().getDrawable(R.drawable.login_button));
        mLoginButton.init(this, mFacebook, PERMISSIONS);
        mLoginButton.setPadding(90, 40, 0, 0);
        
        if(mFacebook.isSessionValid())
        	showValid();
        else{
        	Log.d("loginpage","session not valid");
        }
        //if(ShareTools.isConnectInternet(this))
        //	unsuccessful();
    }
	
	@Override
	protected void onActivityResult (int requestCode, int resultCode, Intent data) {
		finish();
	}
    
    private void init() {
    	_context = this;
    }
/*    
    private void setButtons() {
    	_btLogin.setOnClickListener(actionLogin());
    }
    
    private View.OnClickListener actionLogin() {
    	View.OnClickListener action = new View.OnClickListener() {
    		public void onClick(View v) {
    			Log.d(" Login", "click on login");

		mLoginButton = (LoginButton) this.findViewById(R.id.login_login);
		mText = (TextView) this.findViewById(R.id.login_title);
		// mText = (TextView) Login.this.findViewById(R.id.txt);

		if (!isConnectInternet()) { // no network
			unsuccessful();
			return;
		}

		mFacebook = new Facebook(APP_ID);

		SessionStore.restore(mFacebook, this);
		SessionEvents.addAuthListener(new SampleAuthListener());
		SessionEvents.addLogoutListener(new SampleLogoutListener());
		mLoginButton.setPadding(30, 0, 0, 0);
		mLoginButton.setImageResource(R.drawable.login_button);
		mLoginButton.init(this, mFacebook, PERMISSIONS);

		if (mFacebook.isSessionValid())
			showValid();
	}

	private void init() {
		_context = this;
		/*
		 * _btLogin = (LoginButton) findViewById(R.id.btLogin);
		 * 
		 * _etID = (EditText)findViewById(R.id.etID); _etPW =
		 * (EditText)findViewById(R.id.etPW);
		 */
	

	/*
	 * private void setButtons() { _btLogin.setOnClickListener(actionLogin()); }
	 * 
	 * private View.OnClickListener actionLogin() { View.OnClickListener action
	 * = new View.OnClickListener() { public void onClick(View v) {
	 * Log.d(" Login", "click on login");
	 * 
	 * if(_etID.getText().length() == 0 || _etPW.getText().length() == 0) {
	 * Toast.makeText(Login.this, "Please Enter Your ID and Password",
	 * Toast.LENGTH_SHORT).show(); return; }
	 * 
	 * Log.d(" Login", "password is " + _etPW.getText().toString());
	 * 
	 * Intent change = new Intent(v.getContext(), .class);
	 * change.putExtra("userID", _etID.getText().toString());
	 * startActivity(change); } }; return action; }
	 */
	/*
	 * @Override protected void onActivityResult(int requestCode, int
	 * resultCode, Intent data) { mFacebook.authorizeCallback(requestCode,
	 * resultCode, data); }
	 */

	private void showValid() {
		// share the mFacebook to everyone
		ShareTools.facebook = mFacebook;
		ShareTools.facebookInterface = new Facebook_interface(mFacebook);
		ShareTools.webInterface = new WebInterface();
		ShareTools.uid = ShareTools.facebookInterface.getUID();
		ShareTools.uname = ShareTools.facebookInterface.getName(ShareTools.uid);
		ShareTools.Friends = ShareTools.parsed_friends_list(ShareTools.facebookInterface.getFriend(ShareTools.uid));
		
		mText.setText(ShareTools.uname + " " + getResources().getString(R.string.logged_in));
		mLoginButton.setPadding(10, 0, 0, 0);
		mLoginButton.setImageResource(R.drawable.icon_facebook);
		mLoginButton.setClickable(false);

		Intent change;
		if(_from.equals("recent"))
			change = new Intent(_context, RecentPage.class);
		else if(_from.equals("camera"))
			change = new Intent(_context, CameraPage.class);
		else if(_from.equals("map"))
			change = new Intent(_context, MapPage.class);
		else // if(_from.equals("login"))
			change = null;

		if(change != null)
			startActivityForResult(change, 0);
		//finish();
	}

	private void unsuccessful() {
		mText.setText(getResources().getString(R.string.no_network));
		mLoginButton.setPadding(50, 0, 0, 0);
		mLoginButton.setBackgroundDrawable(null);
		mLoginButton.setImageResource(R.drawable.no_network);
		mLoginButton.setClickable(false);
	}

	public class SampleAuthListener implements AuthListener {
		public void onAuthSucceed() {
			// mText.setText("You have logged in! ");
			Log.d(" Login", "click on login");

			if(mFacebook.isSessionValid())
				showValid();
			else
				Log.d("paradox", "what a paradox");
		}

		public void onAuthFail(String error) {
			// mText.setText("Login Failed: " + error);
			Log.d("login", "login error" + error);
			unsuccessful();
			Toast.makeText(_context, "network is not available",
					Toast.LENGTH_LONG);
		}
	}

	public class SampleLogoutListener implements LogoutListener {
		public void onLogoutBegin() {
			Log.d("logout", "start to logout");
			// mText.setText("Logging out...");
		}

		public void onLogoutFinish() {
			Log.d("logout", "logout ok");
			// mText.setText("You have logged out! ");
		}
	}
}
